package com.example.controller;

import com.example.entity.RestBean;
import com.example.entity.dto.Account;
import com.example.entity.dto.AccountDetails;
import com.example.entity.dto.AccountPrivacy;
import com.example.entity.vo.request.ChangePasswordVO;
import com.example.entity.vo.request.DetailsSaveVO;
import com.example.entity.vo.request.ModifyEmailVO;
import com.example.entity.vo.request.PrivacySaveVO;
import com.example.entity.vo.response.AccountDetailsVO;
import com.example.entity.vo.response.AccountPrivacyVO;
import com.example.entity.vo.response.AccountVO;
import com.example.service.AccountDetailsService;
import com.example.service.AccountPrivacyService;
import com.example.service.AccountService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class AccountController {

    @Resource
    AccountService accountService;

    @Resource
    AccountDetailsService accountDetailsService;

    @Resource
    AccountPrivacyService accountPrivacyService;

    @GetMapping("/info")
    public RestBean<AccountVO> info(@RequestAttribute("id") int id) {
        Account account = accountService.findAccountById(id);
        log.debug("发现的用户={}", account);
        return RestBean.success(new AccountVO(id, account.getUsername(), account.getEmail(), account.getRole(), account.getRegisterTime(), account.getAvatar()));
    }

    @GetMapping("/details")
    public RestBean<AccountDetailsVO> details(@RequestAttribute("id") int id) {
        AccountDetails details = Optional
                                .ofNullable(accountDetailsService.getAccountDetailsById(id))
                                .orElseGet(AccountDetails::new);
        return RestBean.success(new AccountDetailsVO(details.getGender(), details.getPhone(), details.getQq(), details.getWx(), details.getDesc()));
    }

    @PostMapping("/sava-details")
    public RestBean<Void> saveDetails(@RequestAttribute("id") int id, @RequestBody @Valid DetailsSaveVO vo) {
        boolean success = accountDetailsService.saveAccountDetails(id, vo);
        return success ? RestBean.success(null) : RestBean.failure(400,"保存失败");
    }

    @PostMapping("/modify-email")
    public RestBean<Void> modify(@RequestAttribute("id") int id, @RequestBody @Valid ModifyEmailVO vo) {
        String result = accountService.modify(id, vo);
        return result == null ? RestBean.success() : RestBean.failure(400, result);
    }

    @PostMapping("/change-password")
    public RestBean<Void> changePassword(@RequestAttribute("id") int id, @RequestBody @Valid ChangePasswordVO vo) {
        String result = accountService.changePassword(id, vo);
        return result == null ? RestBean.success() : RestBean.failure(400, result);
    }

    @PostMapping("/save-privacy")
    public RestBean<Void> savePrivacy(@RequestAttribute("id") int id, @RequestBody @Valid PrivacySaveVO vo) {
        accountPrivacyService.savePrivacy(id, vo);
        return RestBean.success();
    }

    @GetMapping("/privacy")
    public RestBean<AccountPrivacyVO> privacy(@RequestAttribute("id") int id) {
        AccountPrivacy privacy = accountPrivacyService.accountPrivacy(id);
        AccountPrivacyVO accountPrivacyVO = new AccountPrivacyVO(privacy.isPhone(), privacy.isEmail(), privacy.isGender(), privacy.isQq(), privacy.isWx());
        return RestBean.success(accountPrivacyVO);
    }
}
